package com.dailyblue.java.spring.boot.mvc.mybatis.service.impl;

import com.dailyblue.java.spring.boot.commons.bean.Reply;
import com.dailyblue.java.spring.boot.commons.exception.NotLoginException;
import com.dailyblue.java.spring.boot.commons.util.JsonResult;
import com.dailyblue.java.spring.boot.commons.util.ResultTool;
import com.dailyblue.java.spring.boot.mvc.mybatis.mapper.PostMapper;
import com.dailyblue.java.spring.boot.mvc.mybatis.mapper.ReplyMapper;
import com.dailyblue.java.spring.boot.mvc.mybatis.service.ReplyService;
import com.dailyblue.java.spring.boot.mvc.mybatis.service.UserService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ReplyServiceImpl implements ReplyService {
    @Resource
    private ReplyMapper replyMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UserService userService;
    @Resource
    private PostMapper postMapper;

    @Override
    public JsonResult saveReply(Reply reply, String token) {
        JsonResult<String> result = userService.isLogin(token);
        if (!result.getSuccess()) {
            throw new NotLoginException("");
        }
        int id = Integer.parseInt(result.getData());
        reply.setUserId(id);
        replyMapper.saveReply(reply);
        return ResultTool.success(postMapper.findPostById(reply.getPostId()));
    }
}
